package demo.xlj.yuyi.studentapp.shetuanshencha;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import demo.xlj.yuyi.studentapp.R;
import demo.xlj.yuyi.studentapp.base.BaseRxActivity;
import demo.xlj.yuyi.studentapp.bean.AllowShenQingBean;
import demo.xlj.yuyi.studentapp.bean.LookHuoDongBean;
import demo.xlj.yuyi.studentapp.myadapter.HuoTongAdater;
import demo.xlj.yuyi.studentapp.retrofit.RetrofitClient;
import demo.xlj.yuyi.studentapp.rxjava.RxSchedulers;
import demo.xlj.yuyi.studentapp.utils.SharePerferencesUtils;
import demo.xlj.yuyi.studentapp.utils.ToastUtil;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * 作者：项路俊 on 2018/3/2 0002
 * 邮箱：ThanksForCode@gmail.com
 * 介绍：
 */

public class AllowOrNotHuoDongActivity extends BaseRxActivity {


    @BindView(R.id.ImageView_back)
    ImageView mImageViewBack;
    @BindView(R.id.TextView_Title)
    TextView mTextViewTitle;
    @BindView(R.id.RecyclerView_allow_or_not)
    RecyclerView mRecyclerViewAllowOrNot;

    private List<LookHuoDongBean.InfoBean> mList = new ArrayList<>();
    private HuoTongAdater mHuoTongAdater;


    private String identity = "99";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.allow_or_not_huo_dong_activity);
        ButterKnife.bind(this);
        identity = (String) SharePerferencesUtils.get(AllowOrNotHuoDongActivity.this,"isWho","99",0);
        mTextViewTitle.setText("申请审核");
        initRecyclerView();
        initData();
        initListener();
    }

    private ProgressDialog progressDialog;

    private AlertDialog.Builder mBuilder;

    private EditText mEditText;

    private int positions;

    private void initListener() {

        progressDialog = new ProgressDialog(AllowOrNotHuoDongActivity.this);
        //这是创建方式之一，还有一种，是需要两个参数，参数二是主题，int theme;
        progressDialog.setTitle("请注意：");
        //设置题目
        progressDialog.setCancelable(false);
        progressDialog.setMessage("请耐心等待");
        //设置内容区域

        //拒绝的弹框
        mBuilder = new AlertDialog.Builder(AllowOrNotHuoDongActivity.this);
        mBuilder.setTitle("请输入拒绝原因");
        View vs = LayoutInflater.from(this).inflate(R.layout.edit_text_layout,null,false);
        mEditText = (EditText) vs.findViewById(R.id.editText_get_not_allow_why);
        mBuilder.setView(vs);
        mBuilder.setPositiveButton("提交", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String content = mEditText.getText().toString();
                if (TextUtils.isEmpty(content)){
                    ToastUtil.info(AllowOrNotHuoDongActivity.this,"请输入拒绝原因",false,false);
                }else {
                    addRxDestroy(RetrofitClient.getInstance(AllowOrNotHuoDongActivity.this)
                            .getApiService()
                            .NotShenQing(mList.get(positions).getId()+"",identity,content)
                            .compose(RxSchedulers.<AllowShenQingBean>io_main())
                            .subscribe(new Consumer<AllowShenQingBean>() {
                                @Override
                                public void accept(@NonNull AllowShenQingBean allowShenQingBean) throws Exception {
                                    ToastUtil.success(AllowOrNotHuoDongActivity.this,allowShenQingBean.getInfo().getConcent(),true,true);
                                    progressDialog.dismiss();
                                    mList.remove(positions);
                                    if (mList.size() == 0){
                                        mHuoTongAdater.setEmptyView(R.layout.recycler_item_empty,mRecyclerViewAllowOrNot);
                                    }
                                    mHuoTongAdater.notifyDataSetChanged();
                                }
                            }, new Consumer<Throwable>() {
                                @Override
                                public void accept(@NonNull Throwable throwable) throws Exception {
                                    ToastUtil.error(AllowOrNotHuoDongActivity.this,"操作失败",true,true);
                                    progressDialog.dismiss();
                                }
                            }));
                }
            }
        });

        mBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ToastUtil.info(AllowOrNotHuoDongActivity.this,"您取消了操作",true,true);
            }
        });





        mHuoTongAdater.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.TextView_allow_or_not_ju_jue:
                        positions = position;
                        mBuilder.show();
                        break;

                    case R.id.TextView_allow_or_not_tong_yi:
                        positions = position;
                        progressDialog.show();
                        addRxDestroy(RetrofitClient.getInstance(AllowOrNotHuoDongActivity.this)
                                .getApiService()
                                .allowShenQing(mList.get(positions).getId()+"",identity)
                                .compose(RxSchedulers.<AllowShenQingBean>io_main())
                                .subscribe(new Consumer<AllowShenQingBean>() {
                                    @Override
                                    public void accept(@NonNull AllowShenQingBean allowShenQingBean) throws Exception {
                                        ToastUtil.success(AllowOrNotHuoDongActivity.this,allowShenQingBean.getInfo().getConcent(),true,true);
                                        progressDialog.dismiss();
                                        mList.remove(positions);
                                        if (mList.size() == 0){
                                            mHuoTongAdater.setEmptyView(R.layout.recycler_item_empty,mRecyclerViewAllowOrNot);
                                        }
                                        mHuoTongAdater.notifyDataSetChanged();
                                    }
                                }, new Consumer<Throwable>() {
                                    @Override
                                    public void accept(@NonNull Throwable throwable) throws Exception {
                                        ToastUtil.error(AllowOrNotHuoDongActivity.this,"操作失败",true,true);
                                        progressDialog.dismiss();
                                    }
                                }));
                        break;
                }
            }
        });
    }

    private void initData() {
        mList.clear();
        mHuoTongAdater.setEmptyView(R.layout.recycler_loading,mRecyclerViewAllowOrNot);
        mHuoTongAdater.notifyDataSetChanged();
        if (identity.equals("99")){
            ToastUtil.error(AllowOrNotHuoDongActivity.this,"身份检测失败，请退出在登录",true,true);
        }else {
            addRxDestroy(RetrofitClient.getInstance(AllowOrNotHuoDongActivity.this)
                    .getApiService()
                    .LookHuoDong(identity)
                    .compose(RxSchedulers.<LookHuoDongBean>io_main())
                    .subscribe(new Consumer<LookHuoDongBean>() {
                        @Override
                        public void accept(@NonNull LookHuoDongBean lookHuoDongBean) throws Exception {

                                     if (lookHuoDongBean.getInfo().size() == 0){
                                         mHuoTongAdater.setEmptyView(R.layout.recycler_item_empty,mRecyclerViewAllowOrNot);
                                     }else {
                                         mList.addAll(lookHuoDongBean.getInfo());
                                         mHuoTongAdater.notifyDataSetChanged();
                                     }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(@NonNull Throwable throwable) throws Exception {
                            mHuoTongAdater.setEmptyView(R.layout.recycler_item_error,mRecyclerViewAllowOrNot);
                        }
                    }));
        }

    }

    @OnClick(R.id.ImageView_back)
    public void onViewClicked() {
        AllowOrNotHuoDongActivity.this.finish();
    }

    private void initRecyclerView() {
        mHuoTongAdater = new HuoTongAdater(R.layout.allow_or_not_huo_dong,mList);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(AllowOrNotHuoDongActivity.this);
        mRecyclerViewAllowOrNot.setLayoutManager(layoutManager);

        mHuoTongAdater.setEmptyView(R.layout.recycler_loading,mRecyclerViewAllowOrNot);
        mRecyclerViewAllowOrNot.setAdapter(mHuoTongAdater);

    }
}
